// import Vue from 'vue'
// import Router from 'vue-router'

//解决路由重复跳转报错
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => {
    new VueRouter().go(0)
  })
}

Vue.use(VueRouter)

const router =  new VueRouter({
  routes: [
    {
      path: '/',
      redirect: '/index'
    },
    {
      path: '/index',
      name: 'index',
      leaf:false,
      component: (resolve) => require(['../components/Index/Index.vue'],resolve),
      redirect:'/home',
      children:[{
        path:'/home',
        name:'首页',
        component:(resolve) => require(['../components/Pages/Home.vue'],resolve),
        meta:{title:'首页',breadNumber:1,requireAuth: true,toPath:'/home'}
      },
      {
        path:'/memberlist',
        name:'人员列表',
        component:(resolve) => require(['../components/Pages/Memberlist.vue'],resolve),
        meta:{title:'人员列表',breadNumber:1,requireAuth: true,toPath:'/memberlist'}
      },
      {
        path:'/Membertimelist',
        name:'用户使用时间',
        component:(resolve) => require(['../components/Pages/Membertimelist.vue'],resolve),
        meta:{title:'用户使用时间',breadNumber:1,requireAuth: true,toPath:'/Membertimelist'}
      },
    ]
    },
    {
      path:'/login',
      name:'登录',
      component:resolve => require(['../components/Pages/Login.vue'],resolve)
    }
  ]
})

// 判断是否登录
router.beforeEach((to, from, next) => {
  if (to.matched.some(res => res.meta.requireAuth)) {// 判断是否需要登录权限

    var cookies = document.cookie;
		// 转成数组
		cookies = cookies.split('; ');//['goodslist=[]','your=lemon']
		var ress = '';
		for(var i=0;i<cookies.length;i++){
				// 拆分cookieName和cookieValue
				var arr = cookies[i].split('=');
				if(arr[0] === 'accessToken'){
						ress = arr[1];
						break;
				}
		}
    var log =  ress; 
    if (log) {// 判断是否登录
      next();
    } else {// 
      next();
      // window.location.href = 'https://pxtadmin.com/';
    }
    
  } else {
    next()
  }
 })

export default router
